﻿using Face8280WebSocketServer.WebSockt;
using DoNetDrive.AspWebSocke;
using DoNetDrive.AspWebSocke.WebSocketClient;
using DoNetDrive.AspWebSocke.WebSocketServer;
using DoNetDrive.Core;
using DoNetDrive.Core.Command;
using DoNetDrive.Core.Command.Text;
using DoNetDrive.Core.Connector;
using System.Collections.Concurrent;

namespace Face8280WebSocketServer.Controllers
{
    public static class Allocator
    {
        private static ConnectorAllocator mAllocator { get; set; }

        public static ConcurrentDictionary<string, WebSocketServerClientDetail> WebSocketServerClients = new();

        public static ConcurrentDictionary<string, WebSocketClientDetail> WebSocketClients = new();
        static Allocator()
        {
            var sn = "FC-A103M21116000";
            WebSocketClients.TryAdd(sn, new WebSocketClientDetail(sn, $"ws://192.168.1.130:8601/ws"));
            //替换连接通道工厂
            ConnectorAllocator.DefaultConnectorFactory = new WebSocketConnectorFactory();
            mAllocator = ConnectorAllocator.GetAllocator();
            mAllocator.TransactionMessage += MAllocator_TransactionMessage;
            mAllocator.ClientOnline += MAllocator_ClientOnline;
            mAllocator.ClientOffline += MAllocator_ClientOffline;
        }

        private static void MAllocator_ClientOffline(object sender, ServerEventArgs e)
        {
            Console.WriteLine();
        }

        private static void MAllocator_ClientOnline(object sender, ServerEventArgs e)
        {
            Console.WriteLine();
        }

        public static void AddCommand(INCommand command)
        {
            mAllocator.AddCommand(command);
        }

        public static void AddConnector(string key, INConnector connector)
        {
            mAllocator.AddConnector(key, connector);
        }
        public static INConnector GetConnector(INConnectorDetail detail)
        {
           return mAllocator.GetConnector(detail);
        }
        public static async Task AddCommandAsync(INCommand command)
        {
            await mAllocator.AddCommandAsync(command);
        }
        private static void MAllocator_TransactionMessage(DoNetDrive.Core.Connector.INConnectorDetail connector, DoNetDrive.Core.Data.INData EventData)
        {

        }
        public static async Task< INCommandResult> GetCommandResult(INCommand command)
        {
            await mAllocator.AddCommandAsync(command);
            return command.getResult();
        }

        public static INCommandDetail GetCommandDetail(string sn,string ip,int port)
        {
            WebSocketClientDetail connDtl = new WebSocketClientDetail(sn, $"ws://{ip}:{port}/ws");

            var cmdDtl = new TextCommandDetail(connDtl)
            {
                Timeout = 3000
            };
            return cmdDtl;
        }
        public static INCommandDetail GetCommandDetail(WebSocketClientDetail connDtl)
        {
          //  WebSocketClientDetail connDtl = new WebSocketClientDetail(sn, $"ws://{ip}:{port}/ws");

            var cmdDtl = new TextCommandDetail(connDtl)
            {
                Timeout = 3000
            };
            return cmdDtl;
        }
        public static INCommandDetail GetCommandDetail(WebSocketServerClientDetail connDtl)
        {
            var cmdDtl = new TextCommandDetail(connDtl)
            {
                Timeout = 3000
            };
            return cmdDtl;
        }
    }
}
